home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / gui / mpgui50.lha / sg / RunMPGui.c
C/C++ Source or Header  |  1996-05-08  |  8KB  |  317 lines

  1. // MPGui - requester library
  2. // Copyright (C) © 1995 Mark John Paddock
  3.  
  4. // mark@topic.demon.co.uk
  5. // mpaddock@cix.compulink.co.uk
  6.  
  7. // This source is freely distributable
  8.  
  9. #include <proto/exec.h>
  10. #include <proto/dos.h>
  11. #include <proto/intuition.h>
  12. #include <proto/amigaguide.h>
  13. extern struct Library *AmigaGuideBase = NULL;
  14.  
  15. #include <dos/dos.h>
  16.  
  17. #include <libraries/MPGui.h>
  18. #include <pragmas/MPGui_pragmas.h>
  19. #include <clib/MPGui_protos.h>
  20. #include <dos.h>
  21. #include <string.h>
  22. #include <dos/rdargs.h>
  23. #include <dos/dostags.h>
  24.  
  25. #define TEMPLATE "FROM/A,TO/K,RELMOUSE/S,PUBSCREEN/K,HELP/K,CHELP/S,NEWLINE/S,PREFS/S,BUTTONS/K,NOBUTTONS/S,PARAMS/K/M"
  26.  
  27. #define OPT_FILE            0
  28. #define OPT_TO                1
  29. #define OPT_MOUSE            2
  30. #define OPT_SCREEN        3
  31. #define OPT_HELP            4
  32. #define OPT_CHELP            5
  33. #define OPT_NEWLINE        6
  34. #define OPT_PREFS            7
  35. #define OPT_BUTTONS        8
  36. #define OPT_NOBUTTONS    9
  37. #define OPT_PARAMS        10
  38.  
  39. #define OPT_COUNT            11
  40.  
  41. extern long __oslibversion=39;
  42.  
  43. struct Library *MPGuiBase;
  44.  
  45. const char Version[]="$VER: RunMPGui 5.0 (6.5.96)";
  46.  
  47. AMIGAGUIDECONTEXT     handle     = NULL;
  48. struct NewAmigaGuide    nag         = {NULL};
  49. struct AmigaGuideMsg *agm;            // message from amigaguide
  50. ULONG ASig = 0;
  51.  
  52. int sprintf(char *buffer,char *ctl, ...);
  53.  
  54. extern long __stack = 16000;
  55.  
  56. extern char *ButtonExe=NULL;
  57.  
  58. static char ButtonExe1[256];
  59.  
  60. struct Hook SigHook = {
  61.     0
  62. };
  63.  
  64. ULONG __saveds __asm MyButtons(register __a0 struct Hook *hook,
  65.                                         register __a2 struct MPGuiHandle *gh,
  66.                                         register __a1 ULONG ButtonNo) {
  67.     LONG error;
  68.     sprintf(ButtonExe1,ButtonExe,gh,ButtonNo);
  69.     error = SystemTags(ButtonExe1,
  70.                         SYS_Input,    NULL,
  71.                         SYS_Output,    NULL,
  72.                         NP_Name,        "RunMPGui SubTask",
  73.                         TAG_END);
  74.     if (-1 == error) {
  75.         return 0;
  76.     }
  77.     if (error) {
  78.         return 0;
  79.     }
  80.     return 1;
  81. }
  82.  
  83. ULONG __saveds __asm MySig(register __a0 struct Hook *hook,
  84.                                   register __a2 ULONG signal,
  85.                                   register __a1 ULONG notused) {
  86.     while (agm = GetAmigaGuideMsg(handle)) {
  87.         ReplyAmigaGuideMsg(agm);
  88.     }
  89.     return 1;
  90. }
  91.  
  92. ULONG __saveds __asm Help(register __a0 struct Hook *hook,
  93.                                   register __a2 char *name,
  94.                                   register __a1 APTR notused) {
  95.     char buffer[256];
  96.     while (agm = GetAmigaGuideMsg(handle)) {
  97.         ReplyAmigaGuideMsg(agm);
  98.     }
  99.     sprintf(buffer,"LINK %s",name);
  100.     SendAmigaGuideCmdA(handle,buffer,NULL);
  101.     while (agm = GetAmigaGuideMsg(handle)) {
  102.         ReplyAmigaGuideMsg(agm);
  103.     }
  104.     return 1;
  105. }
  106.  
  107. /****** MPGui.library/--RunMPGui-- ******************************************
  108. *
  109. * RunMPGui provides a simple Shell interface to MPGui.library
  110. *
  111. * Parameters are:
  112. *
  113. * FROM/A       Input GUI file
  114. * TO/K         Output file
  115. * RELMOUSE/S   Open reqester by the pointer
  116. * PUBSCREEN/K  Specify the public screen to open on
  117. * HELP/K       AmigaGuide file to show help
  118. * CHELP/S      Show help continuously
  119. * NEWLINE/S    Put a new line between each gadget
  120. * PREFS/S      Show Save/Use/Cancel gadgets rather than OK/Cancel
  121. * BUTTONS/K    Command to run when a button is pressed
  122. * NOBUTTONS/S  Do not show OK/Cancel gadgets
  123. * PARAMS/K/M   Parameters to substitute in GUI file
  124. *
  125. * The BUTTON command should be specified as "command %ld %ld"
  126. *
  127. * The command is then passed two numbers - the address of the handle and
  128. * the number of the button.
  129. *
  130. * Return a failure from this command to Cancel the GUI.
  131. *
  132. * The response from MPGui is:
  133. *
  134. *   0 if OK is pressed;
  135. *   5 if Cancel is pressed;
  136. *  10 if there is an error in the GUI file;
  137. *  20 if there is some fatal error.
  138. *
  139. *
  140. * Version 5 - Refreshes windows when a requester is open
  141. *             Tries amigaguide.library version 34d
  142. *
  143. *****************************************************************************
  144. *
  145. */
  146.  
  147. int
  148. main(int argc,char **argv) {
  149.     struct MPGuiHandle *MPGuiHandle;
  150.     char *res;
  151.     BOOL FromWB = FALSE;
  152.     struct RDArgs *rdargs = NULL;
  153.     long opts[OPT_COUNT] = {
  154.         0
  155.     };
  156.     int resx = RETURN_OK;
  157.     struct EasyStruct es = {
  158.         sizeof(struct EasyStruct),
  159.         0,
  160.         "RunMPGui",
  161.         NULL,
  162.         "Ok"
  163.     };
  164.     BPTR fh;
  165.     struct Hook HelpHook = {
  166.         0
  167.     };
  168.     struct Hook ButtonHook = {
  169.         0
  170.     };
  171.  
  172.     if (argc == 0) {
  173.         argc = _WBArgc;
  174.         argv = _WBArgv;
  175.         FromWB = TRUE;
  176.         if (argc < 2) {
  177.             es.es_TextFormat = (char *)"Double Click with a Gui file to run";
  178.             EasyRequestArgs(NULL,&es,NULL,NULL);
  179.             return RETURN_WARN;
  180.         }
  181.     }
  182.     else {
  183.         if (!(rdargs = ReadArgs((char *)TEMPLATE, opts, NULL))) {
  184.             PrintFault(IoErr(), NULL);
  185.             return RETURN_ERROR;
  186.         }
  187.     }
  188.     AmigaGuideBase = OpenLibrary("amigaguide.library",34);
  189.     if (MPGuiBase = OpenLibrary("MPGui.library",5)) {
  190.         if (opts[OPT_HELP]) {
  191.             if (AmigaGuideBase) {
  192.                 nag.nag_BaseName        = "RunMPGui";
  193.                 nag.nag_Name            = (char *)opts[OPT_HELP];
  194.                 nag.nag_ClientPort    = "RunMPGui_HELP";
  195.                 nag.nag_Flags            = HTF_NOACTIVATE;
  196.                 nag.nag_PubScreen     = (char *)opts[OPT_SCREEN];
  197.                 handle = OpenAmigaGuideAsync(&nag, TAG_END);
  198.             }
  199.         }
  200.         if (handle) {
  201.             ASig = AmigaGuideSignal(handle);
  202.             HelpHook.h_Entry = (HOOKFUNC)Help;
  203.             while (agm = GetAmigaGuideMsg(handle)) {
  204.                 ReplyAmigaGuideMsg(agm);
  205.             }
  206.         }
  207.         ButtonHook.h_Entry = (HOOKFUNC)MyButtons;
  208.         ButtonExe = (char *)opts[OPT_BUTTONS];
  209.         SigHook.h_Entry = (HOOKFUNC)MySig;
  210.         if (MPGuiHandle = AllocMPGuiHandle(MPG_RELMOUSE, opts[OPT_MOUSE],
  211.                                                     MPG_PUBSCREENNAME,opts[OPT_SCREEN],
  212.                                                     MPG_HELP,handle ? (ULONG)&HelpHook : NULL,
  213.                                                     MPG_CHELP,opts[OPT_CHELP],
  214.                                                     MPG_NEWLINE,opts[OPT_NEWLINE],
  215.                                                     MPG_PREFS,opts[OPT_PREFS],
  216.                                                     MPG_PARAMS, opts[OPT_PARAMS],
  217.                                                     MPG_SIGNALS,    ASig,
  218.                                                     MPG_SIGNALHOOK,(ULONG)&SigHook,
  219.                                                     MPG_BUTTONHOOK, opts[OPT_BUTTONS] ? (ULONG)&ButtonHook : NULL,
  220.                                                     MPG_NOBUTTONS, opts[OPT_NOBUTTONS] ? TRUE : FALSE,
  221.                                                     TAG_END)) {
  222.             if (FromWB) {
  223.                 res = SyncMPGuiRequest(argv[1],MPGuiHandle);
  224.             }
  225.             else {
  226.                 res = SyncMPGuiRequest((char *)opts[OPT_FILE],MPGuiHandle);
  227.             }
  228.             if (res == (char *)-1) {
  229.                 if (FromWB) {
  230.                     es.es_TextFormat = (char *)MPGuiError(MPGuiHandle);
  231.                     EasyRequestArgs(NULL,&es,NULL,NULL);
  232.                 }
  233.                 else {
  234.                     Printf(MPGuiError(MPGuiHandle));
  235.                 }
  236.                 resx = RETURN_ERROR;
  237.             }
  238.             else {
  239.                 if (res) {
  240.                     if (opts[OPT_TO]) {
  241.                         if (fh = Open((char *)opts[OPT_TO],MODE_NEWFILE)) {
  242.                             Write(fh,res,strlen(res));
  243.                             Close(fh);
  244.                         }
  245.                         else {
  246.                             if (FromWB) {
  247.                                 es.es_TextFormat = (char *)"Error opening output file '%s'";
  248.                                 EasyRequest(NULL,&es,NULL,(char *)opts[OPT_TO]);
  249.                             }
  250.                             else {
  251.                                 Printf((char *)"Error opening file '%s'\n",(char *)opts[OPT_TO]);
  252.                             }
  253.                             resx = RETURN_FAIL;
  254.                         }
  255.                     }
  256.                     else {
  257.                         if (FromWB) {
  258.                             es.es_TextFormat = res;
  259.                             EasyRequestArgs(NULL,&es,NULL,NULL);
  260.                         }
  261.                         else {
  262.                             Printf((char *)res);
  263.                             Printf((char *)"\n");
  264.                         }
  265.                     }
  266.                 }
  267.                 else {
  268.                     if (!opts[OPT_TO]) {
  269.                         if (FromWB) {
  270.                             es.es_TextFormat = (char *)"Requester Canceled";
  271.                             EasyRequestArgs(NULL,&es,NULL,NULL);
  272.                         }
  273.                         else {
  274.                             Printf((char *)"Requester Canceled\n");
  275.                         }
  276.                     }
  277.                     resx = RETURN_WARN;
  278.                 }
  279.             }
  280.             FreeMPGuiHandle(MPGuiHandle);
  281.         }
  282.         else {
  283.             if (FromWB) {
  284.                 es.es_TextFormat = (char *)"Error allocating MPGuiHandle";
  285.                 EasyRequestArgs(NULL,&es,NULL,NULL);
  286.             }
  287.             else {
  288.                 Printf((char *)"Error allocating MPGuiHandle\n");
  289.             }
  290.         }
  291.         if (handle) {
  292.             while (agm = GetAmigaGuideMsg(handle)) {
  293.                 ReplyAmigaGuideMsg(agm);
  294.             }
  295.             CloseAmigaGuide(handle);
  296.         }
  297.         CloseLibrary(MPGuiBase);
  298.     }
  299.     else {
  300.         if (FromWB) {
  301.             es.es_TextFormat = (char *)"Error opening MPGui.library(5)";
  302.             EasyRequestArgs(NULL,&es,NULL,NULL);
  303.         }
  304.         else {
  305.             Printf((char *)"Error opening MPGui.library(5)\n");
  306.         }
  307.         resx = RETURN_FAIL;
  308.     }
  309.     if (AmigaGuideBase) {
  310.         CloseLibrary(AmigaGuideBase);
  311.     }
  312.     if (rdargs) {
  313.         FreeArgs(rdargs);
  314.     }
  315.     return resx;
  316. }
  317.